//! Flight SQL集成测试
//! 
//! 测试Flight SQL服务器和客户端的完整集成

use std::sync::Arc;

use data_gateway_kernel::init_kernel;
use data_gateway_flight_sql::{
    server::{FlightSqlServer, FlightSqlServerConfig}
};

#[tokio::test]
async fn test_flight_sql_server_startup() {
    // 创建内核
    let kernel = Arc::new(init_kernel().await.unwrap());
    
    // 创建服务器配置
    let config = FlightSqlServerConfig {
        bind_address: "127.0.0.1:50052".parse().unwrap(), // 使用不同端口避免冲突
        enable_tls: false,
        tls_cert_path: None,
        tls_key_path: None,
        max_concurrent_connections: 100,
        request_timeout_seconds: 30,
    };
    
    // 创建服务器
    let server = FlightSqlServer::new(config, kernel);
    
    // 验证服务器创建成功
    assert_eq!(server.config().bind_address.port(), 50052);
    assert!(!server.config().enable_tls);
}



#[tokio::test]
async fn test_flight_sql_service_components() {
    use data_gateway_flight_sql::service_simple::SimpleFlightSqlService;

    // 创建内核
    let kernel = Arc::new(init_kernel().await.unwrap());

    // 创建Flight SQL服务
    let _service = SimpleFlightSqlService::new(kernel);

    // 只测试服务创建成功
    assert!(true);
}

#[tokio::test]
async fn test_flight_sql_schema_creation() {
    use arrow::datatypes::{Schema, Field, DataType};

    // 创建测试schema
    let schema = Schema::new(vec![
        Field::new("id", DataType::Int64, false),
        Field::new("name", DataType::Utf8, true),
        Field::new("active", DataType::Boolean, true),
    ]);

    // 验证schema创建成功
    assert_eq!(schema.fields().len(), 3);
    assert_eq!(schema.field(0).name(), "id");
    assert_eq!(schema.field(1).name(), "name");
    assert_eq!(schema.field(2).name(), "active");
}

#[tokio::test]
async fn test_flight_sql_query_result_types() {
    use data_gateway_kernel::query::QueryResultData;

    // 测试不同类型的查询结果创建

    // 1. JSON结果
    let json_result = QueryResultData::Json(serde_json::json!({
        "message": "success",
        "count": 42
    }));

    match json_result {
        QueryResultData::Json(_) => assert!(true),
        _ => assert!(false, "应该是JSON类型"),
    }

    // 2. 原始字符串结果
    let raw_result = QueryResultData::Raw("Hello, Flight SQL!".to_string());
    match raw_result {
        QueryResultData::Raw(text) => assert_eq!(text, "Hello, Flight SQL!"),
        _ => assert!(false, "应该是Raw类型"),
    }

    // 3. 空结果
    let empty_result = QueryResultData::Empty;
    match empty_result {
        QueryResultData::Empty => assert!(true),
        _ => assert!(false, "应该是Empty类型"),
    }
}

// 注意：完整的服务器-客户端集成测试需要在实际环境中运行
// 因为需要启动真实的gRPC服务器，这在单元测试中比较复杂
// 这些测试主要验证各个组件的功能正确性

#[tokio::test]
async fn test_flight_sql_record_batch_creation() {
    use arrow::array::{StringArray, Int64Array};
    use arrow::datatypes::{Schema, Field, DataType};
    use arrow::record_batch::RecordBatch;

    // 创建测试RecordBatch
    let schema = Schema::new(vec![
        Field::new("id", DataType::Int64, false),
        Field::new("name", DataType::Utf8, true),
    ]);

    let id_array = Int64Array::from(vec![1, 2, 3]);
    let name_array = StringArray::from(vec![Some("Alice"), Some("Bob"), None]);

    let record_batch = RecordBatch::try_new(
        Arc::new(schema),
        vec![Arc::new(id_array), Arc::new(name_array)]
    ).unwrap();

    // 验证RecordBatch创建成功
    assert_eq!(record_batch.num_rows(), 3);
    assert_eq!(record_batch.num_columns(), 2);
    assert_eq!(record_batch.schema().field(0).name(), "id");
    assert_eq!(record_batch.schema().field(1).name(), "name");
}
